<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/u19.png"/>
        <span el-icon-s-operation>后台管理系统</span>
      </div>
      <el-row style="margin-top: 0.5%;margin-right: -1150px">
          <el-dropdown>
      <span class="el-dropdown-link">
          <img style="width: 50px;height: 50px;border-radius: 50%" :src="empHeaderImgUrl+avatar">
      </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-user-solid"><span @click="toPersonalCenter">个人中心</span></el-dropdown-item>
              <el-dropdown-item icon="el-icon-help">帮助中心</el-dropdown-item>
              <el-dropdown-item icon="el-icon-switch-button"><span @click="logout">注销用户</span></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
      </el-row>
      <span style="font-size: 20px;margin-left: -25px;margin-top: 10px">{{ empName }}</span>
    </el-header>

    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleClapse" style="font-size: 15px">
          <a href="#" ><img src="../assets/ss.png"></a>
        </div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item
                :index="subMenu.url+''"
                v-for="subMenu in item.children"
                :key="subMenu.id"
            >
              <template slot="title">
                <i class="el-icon-paperclip"></i>
                <span>{{ subMenu.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        <el-button type="danger" plain icon="el-icon-switch-button" @click="logout" style="margin: 20px 0 0 40px " title="退出"></el-button>

      </el-aside>
      <el-main>
        <!--在Home组件中点击路由时，在这里显示路由指定的组件-->
        <router-view v-if="isRouterAlive"/>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
export default {
  data() {
    return {
      avatar:'',
      empName: '',
      activeIndex2: '1',
      empHeaderImgUrl : this.$HRMSImageUrl,
      menuList: [
        {
          id: 1,
          name: "用户管理~~~~",
          children: [{id: 2, name: "用户列表", url: "/employee"}]
        },
        {
          id: 4,
          name: "部门管理",
          children: [{id: 5, name: "部门列表", url: "/dept"}]
        },
        {
          id: 7,
          name: "权限管理",
          children: [{id: 8, name: "权限列表", url: "/permission"}]
        },
        {
          id: 10,
          name: "角色管理",
          children: [{id: 11, name: "角色列表", url: "/sysrole"}]
        },
        {
          id: 20,
          name: "课程管理",
          children: [{id: 21, name: "课程列表", url: "/courseList"}]
        }
      ],
      isCollapse: false,
      isRouterAlive: true
    };
  },
  methods: {
    toPersonalCenter(){
      this.$router.push("/PersonalCenter")
    },
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },

    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },

    toggleClapse() {
      this.isCollapse = !this.isCollapse;
    },

    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    initUserName() {
      let info = sessionStorage.getItem("managerInfo");
      if (info) {
        let manager = JSON.parse(info);
        this.empName = manager.empName;
        this.avatar = manager.avatar;
      }
    },

    loadMenus() {
      let name = this.empName;
      let url = "";
      if(name == "admin"){
        url = this.$HRMSUrl + "/permission/listAllMenu";
      }else {
        url = this.$HRMSUrl + "/permission/searchRoleMenu/"+name;
      }
      this.$axios.get(url)
          .then((res) => {
            console.log(res)
            if (res.data.code === 200) {
              this.menuList = res.data.data;
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
                duration: 1000
              })
            }
          })
    }
  },


  created() {
    this.initUserName();
    this.loadMenus();
  }
};

</script>

<style lang="less" scoped>

.el-row {
  margin-top: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}


img {
  width: 30px;
  height: 30px;
}

.toggle-button {
  background-color: #373d48;
  font-size: 10px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 22px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 14px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}
</style>